package cn.gdcp.volley_use_seconde;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    private TextView txt_result;
    //构建一个请求队列
    RequestQueue requestQueue;
    private TextView day1;
    private TextView day2;
    private TextView day3;
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
   // private ArrayList<Weather> weatherArrayList=new ArrayList<>();
   // private ListView weatherListView;
    //private MyAdapter myAdapter;
    //private List<Weather> weatherList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestQueue=Volley.newRequestQueue(MainActivity.this);

        txt_result=findViewById(R.id.txt_result);
//        img_result=findViewById(R.id.img_result);
//        weatherListView=findViewById(R.id.weather_list);
//        myAdapter=new MyAdapter(MainActivity.this,weatherArrayList);
//        weatherList.setAdapter(myAdapter);
        day1=findViewById(R.id.day1);
        day2=findViewById(R.id.day2);
        day3=findViewById(R.id.day3);
        img1=findViewById(R.id.img1);
        img2=findViewById(R.id.img2);
        img3=findViewById(R.id.img3);


        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //useVolleyStringRequest();
                //useVolleyStringRequest("15802080846");
                useVolleyStringRequestPOST("15802080846");
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                useVolleyJsonRequest();
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useVolleyQueryWeather();
            }
        });

    }

    private void useVolleyQueryWeather() {
        String url="http://t.weather.sojson.com/api/weather/city/101280101";
        JsonObjectRequest jsonObjectReq=new JsonObjectRequest(url,null,
                new Response.Listener<JSONObject>(){
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e(TAG,jsonObject.toString());
                        try {
                               JSONObject data=new JSONObject(jsonObject.getString("data"));
                               JSONArray forecast=data.getJSONArray("forecast");
                           // for(int i=1;i<4;i++){
                               JSONObject person1= (JSONObject) forecast.get(1);
                               String type1=person1.getString("type");
                               day1.setText("未来第"+1+"天："+type1);
                               img1.setImageResource(img(type1));

                               JSONObject person2= (JSONObject) forecast.get(2);
                               String type2=person2.getString("type");
                               day2.setText("未来第"+2+"天："+type2);
                               img2.setImageResource(img(type2));

                               JSONObject person3= (JSONObject) forecast.get(3);
                               String type3=person3.getString("type");
                               day3.setText("未来第"+3+"天："+type3);
                               img3.setImageResource(img(type3));
                              // String day="未来第"+i+"天："+type;
                              // Weather w=new Weather(day,img(type));
                               //weatherList.add(w);
                        //  }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        });
        requestQueue.add(jsonObjectReq);
    }

    private int img(String type) {
        if(type.equals("阴")){
            return R.drawable.yin;
        }else if(type.equals("多云")){
            return R.drawable.cloudy;
        }else{
            return R.drawable.sunny;
        }
    }

    //天气接口的地址
    //http://t.weather.sojson.com/api/weather/city/101280101
    private void useVolleyJsonRequest() {
        String url="http://t.weather.sojson.com/api/weather/city/101280101";
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(url,null,
                new Response.Listener<JSONObject>(){
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e(TAG,jsonObject.toString());
                        try {
                            String wendu=jsonObject.getJSONObject("data").getString("wendu");
                            String quality=jsonObject.getJSONObject("data").getString("quality");
                            int pm25=jsonObject.getJSONObject("data").getInt("pm25");
                            StringBuffer stringBuffer=new StringBuffer();
                            stringBuffer.append("温度：").append(wendu).append("\n");
                            stringBuffer.append("质量：").append(quality).append("\n");
                            stringBuffer.append("PM2.5：").append(pm25).append("\n");
                            txt_result.setText(stringBuffer.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    private void useVolleyStringRequestPOST(final String phone) {
        String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        //构建一个请求
        StringRequest stringRequest=new StringRequest(Request.Method.POST,phoneBaseUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG,s);
                //当前是不是在主线程  是！
                txt_result.setText(s); //测试
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        }){
            @Override
            protected Map<String,String> getParams() throws AuthFailureError{


                Map<String,String> map=new HashMap<>();
                map.put("mobileCode",phone);
                map.put("userID","");
                return map;

            }
        };
        //将请求添加到请求队列中
        requestQueue.add(stringRequest);
    }

    //private void useVolleyStringRequest() {
    private void useVolleyStringRequest(String phone) {

        //String url="http://www.baidu.com";
        String url="https://blog.csdn.net/guolin_blog/article/details/17482095/";
        String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String phoneUrl=phoneBaseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
        //构建一个请求
        //StringRequest stringRequest=new StringRequest(url, new Response.Listener<String>() {
        StringRequest stringRequest=new StringRequest(phoneUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG,s);
                //当前是不是在主线程  是！
                txt_result.setText(s); //测试
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        });
        //将请求添加到请求队列中
        requestQueue.add(stringRequest);
    }
}
